// Credit to david.bouchard for all the Touch Event code

PImage img;
Driver d;
TouchProcessor touch;
boolean zoomed;
boolean waszoomed;
float tapx;
float tapy;
float transx = 0;
float transy = 0;
boolean ableup = true;
boolean abledown = true;
boolean ableleft = true;
boolean ableright = true;


void setup() {
  touch = new TouchProcessor();
  AppSetup a = new AppSetup();
  a.setitup();
}

void mousePressed() {
  if (!d.current.equals("PTABLE")) {
    d.mouseDriver(mouseX, mouseY);
  }
}

void onBackPressed() {
    if (zoomed) {
      zoomed = false;
      redraw();
      img = loadImage("ptable.png");
      img.resize(screenWidth, screenHeight);
      background(img); 
      transx = 0;
      transy = 0;
    }
  d.keyboardDriver();
}

void keyPressed() {
  /*if (key == CODED && keyCode == KeyEvent.KEYCODE_BACK) {
    if (zoomed) {
      zoomed = false;
      redraw();
      img = loadImage("ptable.png");
      img.resize(screenWidth, screenHeight);
      background(img); 
      transx = 0;
      transy = 0;
    }
  }*/
  d.keyboardDriver();
   
  if (key == CODED && keyCode == KeyEvent.KEYCODE_HOME) {
    keyCode = 2;
    exit();
  }
  redraw();
}

void draw() {
  touch.analyse();
  touch.sendEvents();
  if (zoomed) {
    translate(tapx+(transx/2), tapy+(transy/2));
    scale(2);
    translate(-tapx+(transx/2), -tapy+(transy/2));
    background(img);
  }
  /*
    text(transy, tapx-(transx/2), tapy-(transy/2)+15);
   text(tapx, tapx-(transx/2), tapy-(transy/2)+30);
   text(tapy, tapx-(transx/2), tapy-(transy/2)+45);
   text(screenWidth, tapx-(transx/2), tapy-(transy/2)+60);
   }
   for (int x = 100; x < screenWidth;x=x+100) {
   stroke(200);
   line(x, 0, x, screenHeight);
   }
   line((tapx/2)+5, 0, (tapx/2)+5, screenHeight);
   line((tapx/2)+(screenWidth/2)-5, 0, (tapx/2)+(screenWidth/2)-5, screenHeight);*/
}

void onTap( TapEvent event ) {
  if (d.current.equals("PTABLE")) {
    if (event.isSingleTap()) {
      if (!zoomed) {
        d.mouseDriver(event.x, event.y);
      }
      if (zoomed) {
        d.mouseDriver((event.x/3)+(tapx/1.5)-transx, (event.y/3)+(tapy/1.5)-transy);
        waszoomed = true;
        zoomed = false;
        redraw();
      }
    }  
    if (event.isDoubleTap()) {
      if (!d.tbl.selected) {
        if (!zoomed) {
          tapx = event.x;
          tapy = event.y;
          zoomed = true;
          img = loadImage("ptable.png");
          img.resize(screenWidth, screenHeight);
          redraw();
        }
        else {
          zoomed = false;
          img = loadImage("ptable.png");
          img.resize(screenWidth, screenHeight);
          background(img); 
          transx = 0;
          transy = 0;
        }
      }
    }
  }
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void onFlick( FlickEvent event ) {
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void onDrag( DragEvent event ) {
  if (zoomed) {
    if (!ableleft) {
      if (event.dx < 0) {   
        transx = transx + event.dx;
        ableleft = true;
      }
      transy = transy + event.dy;
    }
    if (!ableright) {
      if (event.dx > 0) {
        transx = transx + event.dx;
        ableright = true;
      }
      transy = transy + event.dy;
    }
    if (!ableup) {
      if (event.dy < 0) {
        transy = transy + event.dy;
        ableup = true;
      }
      transx = transx + event.dx;
    }
    if (!abledown) {
      if (event.dy > 0) {   
        transy = transy + event.dy;
        abledown = true;
      }
      transx = transx + event.dx;
    }
    if (abledown && ableup && ableright && ableleft) {
      transx = transx + event.dx;
      transy = transy + event.dy;
    }
    if (transx >= tapx/1.5) {
      ableleft = false;
      transx = tapx/1.5;
    }
    else {
      ableleft = true;
    }
    if (transy >= tapy/1.5) {
      ableup = false;
      transy = tapy/1.5;
    }
    else {
      ableup = true;
    }
    if ((screenWidth - ((-transx) + (tapx/1.5) + (screenWidth/3))) <= 0) {
      ableright = false;
      transx = -(screenWidth - (tapx/1.5) - (screenWidth/3));
    }
    else {
      ableright = true;
    }
    if ((screenHeight - ((-transy) + (tapy/1.5) + (screenHeight/3))) <= 0) {
      abledown = false;
      transy = -(screenHeight - (tapy/1.5) - (screenHeight/3));
    }
    else {
      abledown = true;
    }
    redraw();
  }
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void onRotate( RotateEvent event ) {
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void onPinch( PinchEvent event ) {
}

boolean surfaceTouchEvent(MotionEvent event) {

  // extract the action code & the pointer ID
  int action = event.getAction();
  int code   = action & MotionEvent.ACTION_MASK;
  int index  = action >> MotionEvent.ACTION_POINTER_ID_SHIFT;

  float x = event.getX(index);
  float y = event.getY(index);
  int id  = event.getPointerId(index);

  // pass the events to the TouchProcessor
  if ( code == MotionEvent.ACTION_DOWN || code == MotionEvent.ACTION_POINTER_DOWN) {
    touch.pointDown(x, y, id);
  }
  else if (code == MotionEvent.ACTION_UP || code == MotionEvent.ACTION_POINTER_UP) {
    touch.pointUp(event.getPointerId(index));
  }
  else if ( code == MotionEvent.ACTION_MOVE) {
    int numPointers = event.getPointerCount();
    for (int i=0; i < numPointers; i++) {
      id = event.getPointerId(i);
      x = event.getX(i);
      y = event.getY(i);
      touch.pointMoved(x, y, id);
    }
  } 

  return super.surfaceTouchEvent(event);
}

